package over;

import java.lang.reflect.Constructor;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Endstart
 * Date: 2021-05-09
 * Time: 9:59
 */
public class TestMain20210509Over {

    public static Object[][] readBoard(List<String> lines) throws Exception {
        Object[][] objects = new Object[8][8];
        for (String line : lines) {
            try {
                String[] strings = line.split(" ");
                if (strings.length != 3 && strings[0].length() != 2) {
                    throw new IllegalArgumentException();
                }
                int row = strings[0].charAt(0) - 'a';
                int col = Integer.parseInt(strings[0].charAt(1) + "") - 1;
                if (row < 0 || row >= 8 || col < 0 || col >= 8) {
                    throw new IllegalArgumentException();
                }

                String className = strings[1];
                Color color = null;
                if (strings[2].equals("b")) {
                    color = Color.BLACK;
                } else if (strings[2].equals("w")) {
                    color = Color.WHITE;
                }
                if (color == null) {
                    throw new IllegalArgumentException();
                }

                Constructor<?> constructor = Class.forName(className).getConstructor(Color.class);
                constructor.setAccessible(true);
                objects[row][col] = constructor.newInstance(color);

            } catch (IllegalArgumentException | ClassNotFoundException | NoSuchMethodException e) {
                throw new Exception(e.getMessage());
            }
        }

        return objects;
    }

    enum Color{
        BLACK, WHITE;
    }
}
